﻿using DevExpress.Web.ASPxTreeView;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Web.UI.WebControls;

namespace EntryExit
{
    public partial class ShiftCalendar : BasePage
    {
        public Dictionary<string, int> CachedShiftData { get; set; }

        public Employee SelectedEmployee { get; set; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this.Master is MainMaster)
            {
                ((MainMaster)this.Master).MasterTreeViewNodeSelected += ShiftCalendar_MasterTreeViewNodeSelected;
            }
        }

        void ShiftCalendar_MasterTreeViewNodeSelected(object source, TreeViewNodeEventArgs e)
        {
            SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
            ReInitDataCache();
        }

        private void ReInitDataCache()
        {
            SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
            Session[SessionKeys.CACHED_SHIFT_CALENDAR_DATA] = null;
            Session[SessionKeys.SHIFT_CALENDAR_SHOWING_DATE] = DateTime.MinValue;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                shiftCalendar.VisibleDate = new DateTime(DateTime.Now.Year, 1, 1);
                ReInitDataCache();
            }
        }

        private void CacheShiftData()
        {
            SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
            shiftViewerPanel.HeaderText = "Shift Details : " + SelectedEmployee.EmployeeName;
            Session[SessionKeys.SHIFT_CALENDAR_SHOWING_DATE] = shiftCalendar.VisibleDate;
            Session[SessionKeys.CACHED_SHIFT_CALENDAR_DATA] = EmployeeManagerContext.GetEmployeeShiftAllocation(
                shiftCalendar.VisibleDate.AddYears(-1), shiftCalendar.VisibleDate.AddYears(1), SelectedEmployee.LPN);
        }

        protected void shiftCalendar_DayRender(object sender, DevExpress.Web.ASPxEditors.DayRenderEventArgs e)
        {
            try
            {
                if ((DateTime)Session[SessionKeys.SHIFT_CALENDAR_SHOWING_DATE] != shiftCalendar.VisibleDate)
                {
                    CacheShiftData();
                }
                int? shiftIdentifier = GetShiftIdForDay(e.Day.DateTime);
                if (shiftIdentifier != null)
                {
                    switch (shiftIdentifier)
                    {
                        case UIConstants.IDENTIFIER_NO_DATA_BEFORE_DOJ:
                            e.Style.BackColor = Color.Aqua;
                            e.Day.ToolTip = "No data, Reason : Before DOJ";
                            break;
                        case UIConstants.IDENTIFIER_NO_SHIFT_DATA_AVAILABLE:
                            e.Style.BackColor = Color.ForestGreen;
                            e.Day.ToolTip = "Defaulted to General Shift, Reason : No data";
                            break;
                        case UIConstants.IDENTIFIER_NO_SHIFT_DATA_AVAILABLE_IN_FUTURE:
                            //Do nothing, we are not expecting future shift marking here
                            break;
                        case 1:
                            e.Style.BackColor = Color.LightSkyBlue;
                            e.Day.ToolTip = "Evening Shift (1) (2:00 PM-11:00 PM)";
                            break;
                        case 2:
                            e.Style.BackColor = Color.Yellow;
                            e.Day.ToolTip = "Morning Shift (6:00 AM-2:00 PM)";
                            break;
                        case 3:
                            e.Style.BackColor = Color.LightPink;
                            e.Day.ToolTip = "Noon Shift (11:30 AM-8:30 PM)";
                            break;
                        case 4:
                            e.Style.BackColor = Color.LightGreen;
                            e.Day.ToolTip = "General Shift(1) (9:00 AM-6:00 PM)";
                            break;
                        case 5:
                            e.Style.BackColor = Color.Gray;
                            e.Day.ToolTip = "Night Shift";
                            break;
                        case 6:
                            e.Style.BackColor = Color.LightBlue;
                            e.Day.ToolTip = "Late Night (1) (3:00 AM-12:00 PM)";
                            break;
                        case 7:
                            e.Style.BackColor = Color.LightCoral;
                            e.Day.ToolTip = "Late Night (2) (4:00 AM-1:00 PM)";
                            break;
                        case 8:
                            e.Style.BackColor = Color.LightCyan;
                            e.Day.ToolTip = "Early Morning (1) (5:30 AM-2:30 PM)";
                            break;
                        case 9:
                            e.Style.BackColor = Color.LightGoldenrodYellow;
                            e.Day.ToolTip = "Early Morning (2) (6:00 AM-3:00 PM)";
                            break;
                        case 10:
                            e.Style.BackColor = Color.LightSalmon;
                            e.Day.ToolTip = "General - 12hour shift (9:00 AM-9:00 PM)";
                            break;

                            //To Do - Handle case 11. I don't have any info with these shifts

                        case 12:
                            e.Style.BackColor = Color.LightSeaGreen;
                            e.Day.ToolTip = "Late Night (3) (4:30 PM-1:30 AM)";
                            break;
                        case 13:
                            e.Style.BackColor = Color.LightSlateGray;
                            e.Day.ToolTip = "Late Night (4) (5:30 PM-2:30 AM)";
                            break;
                        case 14:
                            e.Style.BackColor = Color.Lime;
                            e.Day.ToolTip = "Late Night (5) (6:00 PM-3:00 AM)";
                            break;
                        case 15:
                            e.Style.BackColor = Color.Magenta;
                            e.Day.ToolTip = "Late Night (6) (10:30 PM-6:30 AM)";
                            break;

                            //To Do - Handle case 16,17. I don't have any info with these shifts

                        case 18:
                            e.Style.BackColor = Color.Maroon;
                            e.Day.ToolTip = "General Shift(2) (8:00 AM-5:00 PM)";
                            break;
                        case 19:
                            e.Style.BackColor = Color.MediumAquamarine;
                            e.Day.ToolTip = "Evening Shift (2) (12:00 PM-9:00 PM)";
                            break;
                        case 20:
                            e.Style.BackColor = Color.MediumPurple;
                            e.Day.ToolTip = "Night Shift(1) (9:00 PM-6:00 PM)";
                            break;

                        default:
                            e.Style.BackColor = Color.Red;
                            e.Day.ToolTip = "Unknown Shift";
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private int? GetShiftIdForDay(DateTime shiftDate)
        {
            string dayKey = shiftDate.ToShortDateString();
            if (Session[SessionKeys.CACHED_SHIFT_CALENDAR_DATA] == null)
            {
                return null;
            }
            else
            {
                if (SelectedEmployee.JoiningDate > shiftDate)
                {
                    return UIConstants.IDENTIFIER_NO_DATA_BEFORE_DOJ;
                }
                if (CachedShiftData == null)
                {
                    CachedShiftData = Session[SessionKeys.CACHED_SHIFT_CALENDAR_DATA] as Dictionary<string, int>;
                }
                if (!CachedShiftData.ContainsKey(dayKey))
                {
                    if (shiftDate < DateTime.Now)
                    {
                        return UIConstants.IDENTIFIER_NO_SHIFT_DATA_AVAILABLE;
                    }
                    else
                    {
                        return UIConstants.IDENTIFIER_NO_SHIFT_DATA_AVAILABLE_IN_FUTURE;
                    }
                }
                return CachedShiftData[dayKey];
            }
        }
    }
}